


//
//  NRootViewController.m
//  nncloud
//
//  Created by macfai on 2018/9/17.
//  Copyright © 2018年 youedata. All rights reserved.
//

#import "NRootViewController.h"
#import "NHomeViewController.h"
#import "NBusinessServiceViewController.h"
#import "NMineViewController.h"
#import "NGovDeskViewController.h"
#import "NGovHomeViewController.h"

@interface NRootViewController ()

@property (nonatomic,strong)NHomeViewController *home;
@property (nonatomic,strong)NBusinessServiceViewController *serve;
@property (nonatomic,strong)NMineViewController *mine;
@property (nonatomic,strong)NGovHomeViewController *govHome;
@property (nonatomic,strong)NGovDeskViewController *govDesk;


@end

@implementation NRootViewController

-(NHomeViewController *)home{
    if (!_home) {
        _home = [[NHomeViewController alloc] init];
        _home.title = @"首页";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"首页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _home.tabBarItem=s_bar;
    }
    return _home;
}

-(NBusinessServiceViewController *)serve{
    
    if (!_serve) {
        
        _serve = [[NBusinessServiceViewController alloc] init];
        _serve.title=@"服务";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"服务" image:[UIImage imageNamed:@"serve"] selectedImage:[UIImage imageNamed:@"serve_s"]];
        _serve.tabBarItem=s_bar;
    }
    return _serve;
}

-(NMineViewController *)mine{
    if (!_mine) {
        _mine = [[NMineViewController alloc] init];
        _mine.title = @"我的";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"me"] selectedImage:[UIImage imageNamed:@"me_s"]];
        _mine.tabBarItem=s_bar;
    }
    return _mine;
}

-(NGovDeskViewController *)govDesk{
    
    if (!_govDesk) {
    _govDesk = [[NGovDeskViewController alloc] init];
    _govDesk.title=@"工作台";
    UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"工作台" image:[UIImage imageNamed:@"desk"] selectedImage:[UIImage imageNamed:@"desk_s"]];
    _govDesk.tabBarItem=s_bar;
}

    return _govDesk;
}

-(NGovHomeViewController *)govHome{
    if (!_govHome) {
        _govHome = [[NGovHomeViewController alloc] init];
        _govHome.title=@"政府主页";
        UITabBarItem *s_bar=[[UITabBarItem alloc]initWithTitle:@"政府主页" image:[UIImage imageNamed:@"home"] selectedImage:[UIImage imageNamed:@"home_s"]];
        _govHome.tabBarItem=s_bar;
    }
    return _govHome;
}



- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults]objectForKey:kSaveUserInfoKey];
    YYLog(@"****%@",userInfo[@"userInfoType"]);
    
    if (userInfo) {
        
        NUserModel *model = [NUserModel shareInstance];
        [model setValuesForKeysWithDictionary:userInfo];
        YYLog(@"%@,%@",model.userInfoNickname,[NUserModel shareInstance].userInfoId);
        //自动登录
        [NUserModel shareInstance].isLogin = YES;
        
        ///接口请求是异步的，会有时间差，所以导致首次加载右上角的小红点不显示，因此下面用了个延时配置tabbar
        [self loadUnReadCount];
    }
//   [self setupVCs];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshTab) name:kRefreshNotification object:nil];
    
    ///解决首次加载小红点不显示的问题
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self configTabBarVC];
    });
    
    ///移动该代码的位置
//    [self configTabBarVC];
   
    
}

///获取未读消息数目
- (void)loadUnReadCount
{
    NSDictionary *dict = @{@"messageUserId":[NUserModel shareInstance].userInfoId};
    [NNetworkClient getRequestWithBaseUrl:kUnReadCountUrl setParameters:dict success:^(id responseObject) {
        
        YYLog(@"未读信息:%@",responseObject);
    
        if ([responseObject[@"code"]integerValue]==0) {
            
            [NUserModel shareInstance].badgeCount = responseObject[@"page"][@"count"];
//            [NUserModel shareInstance].badgeCount = @"3";
            YYLog(@"未读:%@",responseObject[@"page"][@"count"]);
        }
        
    } fail:^(NSError *error) {
        YYLog(@"%@",error);
    
        
    } delegater:nil];
}

- (void)refreshTab {
    [self configTabBarVC];
}

- (void)setupVCs{
//    self.viewControllers = @[self.home,self.serve,self.mine,self.govHome,self.govDesk];
//    self.selectedIndex = 0;
}

-(void)configTabBarVC{
    
    
    NSArray * titleArray;
    NSArray * unSelectImageArray;
    NSArray * selectImageArray;
    //登录用户类型
    //userdefault  是 永久性存，单例是跟随着整个程序的生命周期，所以结合起来用更好
    NSString *userType = [[NSUserDefaults standardUserDefaults]objectForKey:kUserType];
    [NUserModel shareInstance].userInfoType = userType;
    if (([userType integerValue]==1)||([userType integerValue]==0)) {
        
        //1企业   0个人
        UINavigationController * nav0 = [[UINavigationController alloc] initWithRootViewController:self.home];
        UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:self.serve];
        UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:self.mine];
        self.viewControllers = @[nav0,nav1,nav2];
        
        titleArray = @[@"首页",
                       @"服务",
                       @"我的"];
        
        unSelectImageArray = @[@"home",
                               @"serve",
                               @"me"];
        selectImageArray = @[@"home_s",
                             @"serve_s",
                             @"me_s"];
        
    }else {
        
        UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:self.govHome];
        UINavigationController * nav4 = [[UINavigationController alloc] initWithRootViewController:self.govDesk];
        self.viewControllers = @[nav3,nav4];
        
        titleArray = @[@"政府主页",
                       @"工作台"];
        unSelectImageArray = @[@"home",@"desk"];
        selectImageArray = @[@"home_s",@"desk_s"];
        
    }
    
//    UINavigationController * nav0 = [[UINavigationController alloc] initWithRootViewController:self.home];
//    UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:self.serve];
//    UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:self.mine];
//    UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:self.govHome];
//    UINavigationController * nav4 = [[UINavigationController alloc] initWithRootViewController:self.govDesk];
    
    

//    self.viewControllers = @[nav0,nav1,nav2,nav3,nav4];
//
//    NSArray *titleArray = @[@"首页",
//                            @"服务",
//                            @"我的",
//                            @"政府主页",
//                            @"工作台"];
    
    
//    NSArray * unSelectImageArray = @[@"home",
//                                     @"serve",
//                                     @"me",
//                                     @"home",
//                                     @"desk"];
    //带描边的
//    NSArray * selectImageArray = @[@"home_s",
//                                   @"serve_s",
//                                   @"me_s",
//                                   @"home_s",
//                                   @"desk_s"];
    
    for (int i=0; i < unSelectImageArray.count; i++) {
        
        NSString * titel = titleArray[i];
        
        UIImage * normal_image = [UIImage imageNamed:unSelectImageArray[i]];
        normal_image = [normal_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        UIImage * selected_image = [UIImage imageNamed:selectImageArray[i]];
        //取消系统对于选中 item 的渲染
        selected_image = [selected_image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        //获取每一个 item
        UITabBarItem *item=[self.tabBar.items objectAtIndex:i];
        
        item = [item initWithTitle:titel
                             image:normal_image
                     selectedImage:selected_image];
        
        //item.imageInsets = UIEdgeInsetsMake(5, 5, -5, -5);
        [item setTitlePositionAdjustment:UIOffsetMake(0, -2)];
        
    }
    
    //设置 tabbar 字体颜色
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithWhite:0.099 alpha:1.000], NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateNormal];
    //字体颜色变化
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:NColorRGB(108.f, 166.f, 248.f), NSFontAttributeName:[UIFont systemFontOfSize:11]} forState:UIControlStateSelected];
    
    //    self.tabBar.backgroundImage = [UIImage imageNamed:@"tabbarBackground"];
    
    self.selectedIndex = 0;
    
}


@end
